<?php
/**
 * @file Imagelibrary.php
 * @author LinLin
 * @date 2016年7月29日 上午10:29:18
 * @brief 
 */
namespace haibao\erp\business;

class ImageLibrary extends \haibao\erp\business\Base{
    
    public static function saveToMySql($uploadUrl,$storagePath){
        $data = new \haibao\erp\data\ImageLibrary();
        return $data->saveImage($uploadUrl, $storagePath);
    }
    
    
    /**
     * @param \haibao\cms\model\data\ImageLibrary $model
     */
    public static function saveUpload($file){
        
        $storagePath = self::getUploadStoragePath($file['name']);
    
        self::generateImage($storagePath, file_get_contents($file['tmp_name']));
        return array(
            'imageUrl' => $storagePath
        );
    }
    
    /**
     * 获取要上传文件的存储路径
     * @return string /upload/2014/1230/1419842617.9557.jpg
     */
    public static function getUploadStoragePath($orgionName){
        $basePath = \haibao\classlibrary\erp\Config::getConfig(\haibao\classlibrary\erp\Config::IMAGE_UPLOAD_BASE_PATH).date('Y').DIRECTORY_SEPARATOR.date('md').DIRECTORY_SEPARATOR.microtime(true);
        $extension = pathinfo($orgionName);
        $basePath .= '.' . strtolower(isset($extension['extension']) ? $extension['extension'] : 'jpg');
        return str_replace(array('/', '\\'), '/', $basePath);
    }
    
    public static function getImageByFilename($filename){
    	$data = new \haibao\erp\data\ImageLibrary();
    	return $data->getImageByFilename($filename);
    }
    
    public static function addImageByModel($imageLibraryModel){
    	$data = new \haibao\erp\data\ImageLibrary();
    	return $data->add($imageLibraryModel);
    }
    
    
    /**
     * @param int/array $id
     */
    public static function getImagesById($imageIds){
        if(!$imageIds){
            throw new BusinessException('图片ID传递错误');
        }
        $data = new \haibao\erp\data\ImageLibrary();
        return $data->getImageById((array)$imageIds);
    }
} 